package com.cookie.meetproject.ui;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cookie.framework.base.BaseBackActivity;
import com.cookie.framework.bmob.BmobManger;
import com.cookie.framework.helper.FileHelper;
import com.cookie.framework.manager.DialogManager;
import com.cookie.framework.utils.LogUtils;
import com.cookie.framework.view.DialogView;
import com.cookie.framework.view.LoadingView;
import com.cookie.meetproject.R;

import java.io.File;

import androidx.annotation.Nullable;
import cn.bmob.v3.exception.BmobException;
import de.hdodenhof.circleimageview.CircleImageView;

//头像上传
public class FirstUploadActivity extends BaseBackActivity implements View.OnClickListener {

    private TextView tvCamera;
    private TextView tvAblum;
    private TextView tvCancel;
    private DialogView photoSelectView;
    private LoadingView loadingView;

    //跳转
    public static void startActivity(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, FirstUploadActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    private CircleImageView ivPhoto;
    private EditText etNickname;
    private Button btnUpload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_upload);
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_photo:
                //显示选择提示框
                DialogManager.getInstance().show(photoSelectView);
                break;
            case R.id.btn_upload:
                uploadPhoto();
                break;
            case R.id.tv_camera:
                DialogManager.getInstance().hide(photoSelectView);
                if (!checkPermissions(Manifest.permission.CAMERA)) {
                    requestPermission(new String[]{Manifest.permission.CAMERA});
                } else {
                    //跳转到相机
                    FileHelper.getInstance().toCamera(this);
                }
                break;
            case R.id.tv_ablum:
                DialogManager.getInstance().hide(photoSelectView);
                FileHelper.getInstance().toAlbum(FirstUploadActivity.this);
                //跳转到相册
                break;
            case R.id.tv_cancel:
                DialogManager.getInstance().hide(photoSelectView);
                break;
        }
    }

    //上传头像
    private void uploadPhoto() {
        String nickName = etNickname.getText().toString().trim();
        loadingView.show();
        BmobManger.getInstance().uploadFirstPhoto(nickName, uploadFile, new BmobManger.OnUploadPhotoListener() {
            @Override
            public void onUpdateDone() {
                loadingView.hide();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onUpdateFail(BmobException e) {
                loadingView.hide();
                LogUtils.i(e.toString());
                Toast.makeText(FirstUploadActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        initPhotoView();
        ivPhoto = (CircleImageView) findViewById(R.id.iv_photo);
        etNickname = (EditText) findViewById(R.id.et_nickname);
        btnUpload = (Button) findViewById(R.id.btn_upload);
        btnUpload.setOnClickListener(this);
        ivPhoto.setOnClickListener(this);
        btnUpload.setEnabled(false);
        etNickname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    btnUpload.setEnabled(uploadFile != null);
                } else {
                    btnUpload.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //初始化选择框
    private void initPhotoView() {

        loadingView = new LoadingView(this);
        loadingView.setLoadingText("正在上传头像...");

        photoSelectView = DialogManager.getInstance().initView(this,
                R.layout.dialog_select_photo, Gravity.BOTTOM);
        tvCamera = (TextView) photoSelectView.findViewById(R.id.tv_camera);
        tvAblum = (TextView) photoSelectView.findViewById(R.id.tv_ablum);
        tvCancel = (TextView) photoSelectView.findViewById(R.id.tv_cancel);
        tvCancel.setOnClickListener(this);
        tvCamera.setOnClickListener(this);
        tvAblum.setOnClickListener(this);
    }

    private File uploadFile = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == FileHelper.CAMEAR_REQUEST_CODE) {
                FileHelper.getInstance().startPhotoZoom(this, FileHelper.getInstance().getTempFile());
            } else if (requestCode == FileHelper.ALBUM_REQUEST_CODE) {
                Uri uri = data.getData();
                if (uri != null) {
                    //获取真实的地址
                    String path = FileHelper.getInstance().getRealPathFromURI(this, uri);
                    if (!TextUtils.isEmpty(path)) {
                        uploadFile = new File(path);
                        FileHelper.getInstance().startPhotoZoom(this, uploadFile);
                    }
                }
            } else if (requestCode == FileHelper.CAMERA_CROP_RESULT) {
                LogUtils.i("CAMERA_CROP_RESULT");
                uploadFile = new File(FileHelper.getInstance().getCropPath());
                LogUtils.i("uploadPhotoFile:" + uploadFile.getPath());
            }
            //设置头像
            if (uploadFile != null) {
                Bitmap bitmap = BitmapFactory.decodeFile(uploadFile.getPath());
                ivPhoto.setImageBitmap(bitmap);
                //判断当前的输入框
                String nickName = etNickname.getText().toString().trim();
                btnUpload.setEnabled(!TextUtils.isEmpty(nickName));
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
